package com.fmsz.xmly.asynctasks.findrecom;

import android.os.AsyncTask;
import android.text.TextUtils;

import com.fmsz.xmly.callbacks.InterfaceClass;
import com.fmsz.xmly.entitys.findrecom.FindRecomVP1Entity;
import com.fmsz.xmly.utils.MyHttpUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/12/3.
 */

public class FindRecomVP1Down extends AsyncTask<String,Void,String> {
    InterfaceClass.FindRecomVP1Callback callback;

    public FindRecomVP1Down(InterfaceClass.FindRecomVP1Callback callback) {
        this.callback = callback;
    }

    @Override
    protected String doInBackground(String... params) {
        String json= MyHttpUtils.getStringFromUrl(params[0]);
        return json;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        if (!TextUtils.isEmpty(s)) {
            List<FindRecomVP1Entity> list=getList(s);
            callback.findRecomVP1Callback(list);
        }
    }

    private List<FindRecomVP1Entity> getList(String json) {
        List<FindRecomVP1Entity> list=new ArrayList<>();
        try {
            JSONObject obj1=new JSONObject(json);
            JSONObject focusImages = obj1.getJSONObject("focusImages");
            JSONArray list1 = focusImages.getJSONArray("list");
            for (int i = 0; i < list1.length(); i++) {
                JSONObject obj2 = list1.getJSONObject(i);
                String id = obj2.getString("id");
                String pic = obj2.getString("pic");
                String type = obj2.getString("type");
                if (type.equals("2")){
                    String albumId = obj2.getString("albumId");
                    list.add(new FindRecomVP1Entity(id,pic,"2",albumId));
                }else if (type.equals("3")){
                    String trackId = obj2.getString("trackId");
                    list.add(new FindRecomVP1Entity(id,pic,"3",trackId));
                }else if(type.equals("4")){
                    String url = obj2.getString("url");
                    list.add(new FindRecomVP1Entity(id,pic,"4",url));
                }else if (type.equals("9")){
                    String specialId = obj2.getString("specialId");
                    list.add(new FindRecomVP1Entity(id,pic,"9",specialId));
                }
            }
            return list;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
}
